// Copyright (c) 2015 Baidu.com, Inc. All Rights Reserved
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Authors: Zhangyi Chen(chenzhangyi01@baidu.com)

#ifndef  KRAFT_MACROS_H
#define  KRAFT_MACROS_H

#include <krpc/utility/macros.h>
#include <krpc/utility/logging.h>
#include <krpc/var/utils/lock_timer.h>

#define KRAFT_VLOG_IS_ON     VLOG_IS_ON(89)
#define KRAFT_VLOG           VLOG(89)
#define KRAFT_VPLOG          VPLOG(89)
#define KRAFT_VLOG_IF(cond)  VLOG_IF(89, (cond))
#define KRAFT_VPLOG_IF(cond) VPLOG_IF(89, (cond))

//#define USE_KTHREAD_MUTEX

#ifdef USE_KTHREAD_MUTEX

#include <krpc/kthread/mutex.h>

namespace kraft {
typedef ::kthread::Mutex raft_mutex_t;
}  // namespace kraft

#else   // USE_KTHREAD_MUTEX

#include <krpc/utility/synchronization/lock.h>
namespace kraft {
typedef ::kutil::Mutex raft_mutex_t;
}  // namespace kraft

#endif  // USE_KTHREAD_MUTEX

#ifdef UNIT_TEST
#define KRAFT_MOCK virtual
#else
#define KRAFT_MOCK
#endif

#define KRAFT_GET_ARG3(arg1, arg2, arg3, ...)  arg3

#define KRAFT_RETURN_IF1(expr, rc)       \
    do {                                \
        if ((expr)) {                   \
            return (rc);                \
        }                               \
    } while (0)

#define KRAFT_RETURN_IF0(expr)           \
    do {                                \
        if ((expr)) {                   \
            return;                     \
        }                               \
    } while (0)

#define KRAFT_RETURN_IF(expr, args...)   \
        KRAFT_GET_ARG3(1, ##args, KRAFT_RETURN_IF1, KRAFT_RETURN_IF0)(expr, ##args)

#endif  //KRAFT_MACROS_H
